package com.group7.mall.controller;

import com.group7.mall.bean.Result;
import com.group7.mall.convert.AuditConvert;
import com.group7.mall.dao.*;
import com.group7.mall.dto.AdminBase64Dto;
import com.group7.mall.dto.ComplainPreviewDto;
import com.group7.mall.dto.StoreAuditFormPreviewDto;
import com.group7.mall.entity.*;
import com.group7.mall.req.StoreAuditPageRequest;
import com.group7.mall.resp.StoreAuditPageResponse;
import com.group7.mall.service.AdminService;
import com.group7.mall.util.Base64Utils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Base64;

@RequestMapping("admin")
@RestController
public class AdminController {

    @Resource
    private TbAnnouncementDao tbAnnouncementDao;

    @Resource
    private AdminService adminService;

    @Resource
    private TbMessageAuditFormDao tbMessageAuditFormDao;

    @Resource
    private TbSellerDao tbSellerDao;

    @Resource
    private TbUserDao tbUserDao;

    @Resource
    private TbComplainDao tbComplainDao;

    @Resource
    private TbAdminDao tbAdminDao;

    @PostMapping("admin-register")
    public Result<Integer> adminRegister(@RequestBody TbAdmin tbAdmin) {
        tbAdmin.setUserName(Base64Utils.encode(tbAdmin.getUserName()));
        tbAdmin.setUserPassword(Base64Utils.encode(tbAdmin.getUserPassword()));
        return Result.ok(tbAdminDao.insert(tbAdmin));
    }

    @PostMapping("admin-login")
    public Result<Integer> adminLogin(@RequestBody TbAdmin tbAdmin) {
        String userName = Base64Utils.encode(tbAdmin.getUserName());
        String password = Base64Utils.encode(tbAdmin.getUserPassword());
        System.out.println(userName+" "+password);
        tbAdmin = tbAdminDao.queryUserName(userName);
        if (tbAdmin != null) {
            if (!Objects.equals(tbAdmin.getUserPassword(), password)) {
                return Result.ok(0);
            }
            return Result.ok(1);
        } else {
            return Result.ok(-1);
        }
    }

    @PostMapping("show-all-audit")
    public Result<StoreAuditPageResponse<StoreAuditFormPreviewDto>> showAuditByPage(@RequestBody StoreAuditPageRequest storeAuditPageRequest) {
        return Result.ok(adminService.showAuditByPage(storeAuditPageRequest));
    }

    @PostMapping("show-audit-detail")
    public Result<TbStoreAuditForm> showAuditDetail(@RequestBody TbStoreAuditForm tbStoreAuditForm) {
        return Result.ok(adminService.showStoreAuditFormDetail(tbStoreAuditForm));
    }

    @PostMapping("auditing-access")
    public Result<Integer> auditingAccess(@RequestBody TbStoreAuditForm tbStoreAuditForm) {
        return Result.ok(adminService.storeAuditingAccess(tbStoreAuditForm));
    }

    @PostMapping("auditing-not-access")
    public Result<Integer> auditingNotAccess(@RequestBody TbStoreAuditForm tbStoreAuditForm) {
        return Result.ok(adminService.storeAuditingNotAccess(tbStoreAuditForm));
    }

    @PostMapping("show-all-message-audit")
    public Result<StoreAuditPageResponse<StoreAuditFormPreviewDto>> showMessageAuditByPage(@RequestBody StoreAuditPageRequest storeAuditPageRequest) {
        return Result.ok(adminService.showMessageAuditByPage(storeAuditPageRequest));
    }

    @PostMapping("show-message-audit-detail")
    public Result<StoreAuditPageResponse<StoreAuditFormPreviewDto>> showMessageAuditDetail(@RequestBody TbSeller tbSeller) {
        return Result.ok(adminService.showMessageAuditDetail(tbSeller.getStoreId()));
    }

    @PostMapping("message-audit-detail")
    public Result<StoreAuditPageResponse<StoreAuditFormPreviewDto>> messageAudit(@RequestBody TbMessageAuditForm tbMessageAuditForm) {
        Integer auditStatus = tbMessageAuditForm.getAuditStatus();
        if (auditStatus == 1) {
            tbMessageAuditForm = tbMessageAuditFormDao.queryByStoreId(tbMessageAuditForm.getStoreId());
            TbSeller tbSeller = AuditConvert.INSTANCE.convertTbMessageAuditFormToTbSeller(tbMessageAuditForm);
            tbSellerDao.update(tbSeller);
            return Result.ok(tbMessageAuditFormDao.deleteById(tbMessageAuditForm.getAuditId()));
        } else if (auditStatus == 2) {
            return Result.ok(tbMessageAuditFormDao.deleteById(tbMessageAuditFormDao.queryByStoreId(tbMessageAuditForm.getStoreId()).getAuditId()));
        }
        return null;
    }

    @PostMapping("send-complain")
    public Result<Integer> sendComplain(@RequestBody TbComplain tbComplain) {
        String userOpenId = tbComplain.getUserOpenId();
        if (tbSellerDao.queryBySellerId(userOpenId) != null) {
            tbComplain.setIdentity(1);
        } else {
            tbComplain.setIdentity(0);
        }
        return Result.ok(tbComplainDao.insert(tbComplain));
    }

    @PostMapping("show-all-complain")
    public Result<StoreAuditPageResponse<ComplainPreviewDto>> showAllComplain(@RequestBody StoreAuditPageRequest storeAuditPageRequest) {
        StoreAuditPageResponse<ComplainPreviewDto> storeAuditPageResponse = new StoreAuditPageResponse<ComplainPreviewDto>();
        storeAuditPageRequest.setPageStart((storeAuditPageRequest.getPageNo() - 1) * storeAuditPageRequest.getPageSize());
        List<TbComplain> tbComplains = tbComplainDao.queryByPage(storeAuditPageRequest);
        Integer index = storeAuditPageRequest.getIdentity();
        List<ComplainPreviewDto> complainPreviewDtoList = new ArrayList<>();

        if (index == 0) {
            for(TbComplain x: tbComplains) {
                String userOpenId = x.getUserOpenId();
                TbUser tbUser = tbUserDao.queryByUserOpenId(userOpenId);
                ComplainPreviewDto complainPreviewDto = new ComplainPreviewDto();
                complainPreviewDto.setComplainId(x.getComplainId());
                complainPreviewDto.setIdentity(index);
                complainPreviewDto.setComplainContent(x.getComplainContent());
                complainPreviewDto.setUserName(tbUser.getUsername());
                complainPreviewDto.setUserImage(tbUser.getUserImage());
                complainPreviewDtoList.add(complainPreviewDto);
            }
        } else if (index == 1) {
            for(TbComplain x: tbComplains) {
                String userOpenId = x.getUserOpenId();
                TbSeller tbSeller = tbSellerDao.queryBySellerId(userOpenId);
                ComplainPreviewDto complainPreviewDto = new ComplainPreviewDto();
                complainPreviewDto.setComplainId(x.getComplainId());
                complainPreviewDto.setIdentity(index);
                complainPreviewDto.setComplainContent(x.getComplainContent());
                complainPreviewDto.setUserName(tbSeller.getStoreName());
                complainPreviewDto.setUserImage(tbSeller.getStoreImage());
                complainPreviewDtoList.add(complainPreviewDto);
            }
        }

        storeAuditPageResponse.setPageSize(storeAuditPageRequest.getPageSize());
        storeAuditPageResponse.setCurrent(storeAuditPageRequest.getPageNo());
        storeAuditPageResponse.setRecords(complainPreviewDtoList);
        storeAuditPageResponse.setTotal(tbComplainDao.count(new TbComplain()));
        return Result.ok(storeAuditPageResponse);
    }

    @PostMapping("show-complain-detail")
    public Result<TbComplain> showComplainDetail(@RequestBody TbComplain tbComplain) {
        return Result.ok(tbComplainDao.queryById(tbComplain.getComplainId()));
    }


    @PostMapping("user-show-all-complain")
    public Result<List<TbComplain>> UserShowComplainDetail(@RequestBody TbComplain tbComplain) {
        return Result.ok(tbComplainDao.queryByUserOpenId(tbComplain.getUserOpenId()));
    }

    @PostMapping("admin-deal-complain")
    public Result<Integer> adminDealComplain(@RequestBody TbComplain tbComplain) {
        String content = tbComplain.getDealContent();
        tbComplain = tbComplainDao.queryById(tbComplain.getComplainId());
        tbComplain.setDealContent(content);
        System.out.println(tbComplain.toString() + " " + content);
        return Result.ok(tbComplainDao.update(tbComplain));
    }

    @PostMapping("get-announcement")
    public Result<String> getAnnouncement() {
        return Result.ok(tbAnnouncementDao.queryById(0).getAnnouncementContent());
    }

    @PostMapping("update-announcement")
    public Result<String> updateAnnouncement(@RequestBody TbAnnouncement tbAnnouncement) {
        TbAnnouncement tbAnnouncement1 = tbAnnouncementDao.queryById(0);
        tbAnnouncement1.setAnnouncementContent(tbAnnouncement.getAnnouncementContent());
        return Result.ok(tbAnnouncementDao.update(tbAnnouncement1));
    }
}
